#include "voicerec.h"

voiceRec::voiceRec(QObject *parent) : QObject(parent)
{
    setupAudioFormat();
}

voiceRec::~voiceRec()
{

}

void voiceRec::setupAudioFormat()
{
    audioFormat.setSampleRate(16000);
    audioFormat.setChannelCount(1);
    audioFormat.setSampleSize(16);
    audioFormat.setSampleType(QAudioFormat::SignedInt);
    audioFormat.setCodec("audio/pcm");
    audioFormat.setByteOrder(QAudioFormat::LittleEndian);
}

void voiceRec::startRecording()
{
    audioInput = new QAudioInput(audioFormat);
    audioBuffer.open(QIODevice::WriteOnly | QIODevice::Truncate);
    audioInput->start(&audioBuffer);
}

void voiceRec::stopRecording()
{
    audioInput->stop();
    audioBuffer.close();
    QByteArray pcmData = audioBuffer.data();
    emit send_audioData(pcmData);
    audioInput->deleteLater();
}
